import system from '@/models/system';
import { ObjectId } from 'mongodb';

// 新增\修改系统
export const syncAddSystem = async (params: { _id?: string; projectId?: string; systemName?: string; gitlabName?: string; webUrl?: string; env?: string; isEdit?: boolean }) => {
  const { isEdit, _id, ...props } = params;
  const id = new ObjectId(_id);
  if (params.isEdit) {
    const res: any = await system.update(props, { _id: id });
    return res;
  }
  const data = await system.findOne({ projectId: params.projectId, env: params.env });
  if (data) {
    return null;
  }
  const res: any = await system.insert({ ...props });
  return res;
};
// 删除
export const syncDeleteSystem = async (params: { id: string }) => {
  const res: any = await system.deleteOne({ _id: new ObjectId(params.id) });
  return res;
};
// 查询
export const syncGetSystem = async (params: { _id?: string; webUrl?: string; env?: string; page?: number; pageSize?: number; gitlabName?: string }) => {
  const temp: any = {};
  const { _id, webUrl, env, page, pageSize, gitlabName } = params;
  if (webUrl) {
    temp.webUrl = webUrl;
  }
  if (env) {
    temp.env = env;
  }
  if (_id) {
    temp._id = new ObjectId(_id);
  }
  if (gitlabName) {
    temp.gitlabName = gitlabName;
  }
  const res: any = await system.getList(temp, undefined, page, pageSize);
  return res;
};
